package com.hcbd.tdwt.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.ui.xingyu.vo.SatelliteVO;

import java.util.List;

public class GnssSkyView extends View {
    private int mWidth,mHeight;
    public int Y_TRANSLATION=10;
    private Paint mPaintCircle;
    private Paint mPaintDegree;
    private Paint mPaintLine;
    private Paint mPrnIdPaint;
    private Bitmap chinaMap;
    private Bitmap usaMap,eu,russia,other;

    private List<SatelliteVO> satelliteVOList;
    public GnssSkyView(Context context) {
        super(context);
    }
    public GnssSkyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }
    public GnssSkyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaintCircle=new Paint();
        mPaintCircle.setAntiAlias(true);
        mPaintCircle.setStyle(Paint.Style.STROKE);
        mPaintCircle.setColor(context.getResources().getColor(R.color.white));

        mPaintLine=new Paint();
        mPaintLine.setAntiAlias(false);                       //设置画笔为无锯齿
        mPaintLine.setColor(context.getResources().getColor(R.color.white)); //设置画笔颜色
        mPaintLine.setStrokeWidth((float) 1.0);
        mPaintDegree=new Paint();
        mPaintDegree.setAntiAlias(false);                       //设置画笔为无锯齿
        mPaintDegree.setColor(context.getResources().getColor(R.color.white)); //设置画笔颜色
        mPaintDegree.setStrokeWidth((float) 1.0);

        mPrnIdPaint=new Paint();
        mPrnIdPaint.setColor(context.getResources().getColor(R.color.white));
        mPrnIdPaint.setStrokeWidth((float) 1.0);
        mPrnIdPaint.setTextSize(20);
        chinaMap = BitmapFactory.decodeResource(getResources(), R.drawable.china);
        usaMap= BitmapFactory.decodeResource(getResources(), R.drawable.usa);
        eu=BitmapFactory.decodeResource(getResources(), R.drawable.eu);
        russia=BitmapFactory.decodeResource(getResources(),R.drawable.russia);
        other=BitmapFactory.decodeResource(getResources(), R.drawable.mix);
    }
    public void setBgColor(int color){
        mPaintCircle.setColor(color);
        mPaintLine.setColor(color);
        mPaintDegree.setColor(color);
        mPrnIdPaint.setColor(color);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int minScreenDimen = Math.min(mWidth, mHeight);
        float radius = minScreenDimen / 2.0f;
        drawCircle(canvas, radius);
        drawLine(canvas, minScreenDimen, radius);
        drawDegree(canvas, radius);
        if(satelliteVOList!=null) {
            for (SatelliteVO satelliteVO : satelliteVOList) {
                drawSatellite(canvas, minScreenDimen, satelliteVO);
            }
        }
    }
    public void drawBatchSatelliteVO(List<SatelliteVO>list){
        satelliteVOList=list;
        invalidate();

    }
    private void drawCircle(Canvas c, float radius) {
        c.drawCircle(radius, radius + Y_TRANSLATION, elevationToRadius(radius, 60.0f), mPaintCircle);
        c.drawCircle(radius, radius + Y_TRANSLATION, elevationToRadius(radius, 30.0f), mPaintCircle);
        c.drawCircle(radius, radius + Y_TRANSLATION, elevationToRadius(radius, 0.0f), mPaintCircle);
    }

    private float elevationToRadius(float s, float elev) {
        return s * (1.0f - (elev / 90.0f));
    }
    private void drawLine(Canvas c, int s, float radius) {
        c.drawLine(radius, Y_TRANSLATION, radius, s + Y_TRANSLATION, mPaintLine);
        c.drawLine(0, radius + Y_TRANSLATION, s, radius + Y_TRANSLATION, mPaintLine);

        final float cos45 = (float) Math.cos(Math.PI / 4);
        float d1 = radius * (1 - cos45);
        float d2 = radius * (1 + cos45);
        c.drawLine(d1, d1 + Y_TRANSLATION, d2, d2 + Y_TRANSLATION, mPaintLine);
        c.drawLine(d2, d1 + Y_TRANSLATION, d1, d2 + Y_TRANSLATION, mPaintLine);
    }
    private void drawDegree(Canvas c, float radius) {
        for (int i = 0; i < 360; i += 15) {
            if (i == 45 || i == 135 || i == 225 || i == 315) {
                c.drawText(String.valueOf(i), radius, 40 + Y_TRANSLATION, mPaintDegree);
            } else if (i == 0) {
                c.drawText("N", radius, 40 + Y_TRANSLATION, mPaintDegree);
            } else if (i == 90) {
                c.drawText("E", radius, 40 + Y_TRANSLATION, mPaintDegree);
            } else if (i == 180) {
                c.drawText("S", radius, 40 + Y_TRANSLATION, mPaintDegree);
            } else if (i == 270) {
                c.drawText("W", radius, 40 + Y_TRANSLATION, mPaintDegree);
            } else {
                c.drawLine(radius, Y_TRANSLATION, radius, 20 + Y_TRANSLATION, mPaintDegree);
            }

            c.rotate(15, radius, radius + Y_TRANSLATION);
        }
    }
    /* protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);

         int windowWidth = mDisplayMetrics.widthPixels;
         int windowHeight = mDisplayMetrics.heightPixels;

         int minL = Math.min(windowWidth, windowHeight);

         setMeasuredDimension(minL, minL + 20);
     }
     private void init(Context context) {
         mDisplayMetrics = context.getResources().getDisplayMetrics();
         getViewTreeObserver().addOnPreDrawListener(
                 new ViewTreeObserver.OnPreDrawListener() {
                     @Override
                     public boolean onPreDraw() {
                         mHeight = getHeight();
                         mWidth = getWidth();
                         return true;
                     }
                 }
         );
         invalidate();
     }*/
    private int SAT_RADIUS=16;
    private void drawSatellite(Canvas c, int s,SatelliteVO satelliteVO) {
        double radius, angle;
        float x, y;
        Bitmap satMap;
        satMap = getSatelliteBitmap(satelliteVO.getSatelliteType());
        radius = elevationToRadius(s / 2.0f, Integer.parseInt(satelliteVO.getElv()));
        angle = (float) Math.toRadians(Integer.parseInt(satelliteVO.getAz()));
        x = (float) ((s / 2.0f) + (radius * Math.sin(angle)));
        y = (float) ((s / 2.0f) - (radius * Math.cos(angle)));
        c.drawBitmap(satMap, x - SAT_RADIUS, y - SAT_RADIUS + Y_TRANSLATION, null);
        c.drawText(satelliteVO.getSv(), x - SAT_RADIUS, y + SAT_RADIUS * 2 + Y_TRANSLATION, mPrnIdPaint);
    }
    private Bitmap getSatelliteBitmap(String constellationType) {
        if(constellationType.contains("BD")||constellationType.contains("PQ")||constellationType.contains("GB")){
            return chinaMap;
        }else if(constellationType.contains("GP")){
            return usaMap;
        }else if(constellationType.contains("GL")){
           return russia;
        }else if(constellationType.contains("GA")){
           return eu;
        }else{
           return other;
        }
    }



}

